/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package G3.Mod;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import java.io.IOException;

/**
 *
 * @author Dymion
 */
public class data2sd extends Activity implements OnClickListener {

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);
        setContentView(R.layout.data2sd);

        DialogInterface.OnClickListener dialogClickListener = new DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {
                switch (which) {
                    case DialogInterface.BUTTON_POSITIVE:
                        break;
                }
            }
        };

        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setMessage("Make sure your SD-card is partitioned properly.").setPositiveButton("Ok", dialogClickListener).show();
        builder.setCancelable(false);

        TextView data2sdTxt = (TextView) findViewById(R.id.data2sdTxt);
        Button data2sd = (Button) findViewById(R.id.data2sd);
        readfile read = new readfile("/Android/data/g3mod/", "fs.data2sd", true);
        if (read.fileExists()) {
            data2sdTxt.append("Activated.");
            data2sd.setText("Deactivate Data2Sd");
        } else {
            data2sdTxt.append("Deactivated.");
            data2sd.setText("Activate Data2Sd");
        }

        View data2sdBtn = findViewById(R.id.data2sd);
        data2sdBtn.setOnClickListener(this);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        new MenuInflater(this).inflate(R.menu.menu, menu);
        return (super.onCreateOptionsMenu(menu));
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle item selection
        switch (item.getItemId()) {
            case R.id.aboutMenu:
                startActivity(new Intent(this, About.class));
                //finish();
                return true;
            default:
                return super.onOptionsItemSelected(item);
        }
    }

    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.data2sd:
                final TextView data2sdTxt = (TextView) findViewById(R.id.data2sdTxt);
                final Button data2sd = (Button) findViewById(R.id.data2sd);

                readfile read = new readfile("/Android/data/g3mod/", "fs.data2sd", true);
                if (read.fileExists()) {
                    read.fileDelete();

                    DialogInterface.OnClickListener dialogClickListener = new DialogInterface.OnClickListener() {

                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            switch (which) {
                                case DialogInterface.BUTTON_POSITIVE:
                                    try {
                                        Runtime.getRuntime().exec(new String[]{"/system/bin/su", "-c", "reboot now"});
                                    } catch (IOException ex) {
                                        //It doesn't work..
                                    }
                                    break;

                                case DialogInterface.BUTTON_NEGATIVE:
                                    data2sdTxt.setText("Data2Sd Deactivated.");
                                    data2sd.setText("Activate Data2Sd");
                                    break;
                            }
                        }
                    };

                    AlertDialog.Builder builder = new AlertDialog.Builder(this);
                    builder.setMessage("Data2Sd deactivated. \n Reboot now?").setPositiveButton("Yes", dialogClickListener).setNegativeButton("No", dialogClickListener).show();
                    builder.setCancelable(false);
                } else {
                    writefile write = new writefile("mmcblk0p2", "/Android/data/g3mod/", "fs.data2sd", true);

                    DialogInterface.OnClickListener dialogClickListener = new DialogInterface.OnClickListener() {

                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            switch (which) {
                                case DialogInterface.BUTTON_POSITIVE:
                                    try {
                                        Runtime.getRuntime().exec(new String[]{"/system/bin/su", "-c", "reboot now"});
                                    } catch (IOException ex) {
                                        //It doesn't work..
                                    }
                                    break;

                                case DialogInterface.BUTTON_NEGATIVE:
                                    data2sdTxt.setText("Data2Sd Activated.");
                                    data2sd.setText("Deactivate Data2Sd");
                                    break;
                            }
                        }
                    };

                    AlertDialog.Builder builder = new AlertDialog.Builder(this);
                    builder.setMessage("Data2Sd activated. \n Reboot now?").setPositiveButton("Yes", dialogClickListener).setNegativeButton("No", dialogClickListener).show();
                    builder.setCancelable(false);
                }
                break;
        }
    }
}
